home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '93 / Papers '93 / Macintosh as Internet Server ƒ / inetd / Libraries / DaemonApp / PascalString.cp < prev    next >
Encoding:
Text File  |  1993-04-07  |  17.5 KB  |  597 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // PascalString.cp 
  3. // Copyright © 1985-1992 by Apple Computer, Inc.  All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6.  
  7. #include "PascalString.h"
  8.  
  9. #ifndef __STDIO__
  10. #include <StdIo.h>
  11. #endif
  12.  
  13. #ifndef __STRING__
  14. #include <String.h>
  15. #endif
  16.  
  17.  
  18. #pragma segment Main
  19.  
  20.  
  21. //========================================================================================
  22. // CLASS CString
  23. //========================================================================================
  24.  
  25.  
  26. //----------------------------------------------------------------------------------------
  27. // CString::InsertHelper(CString):
  28. //----------------------------------------------------------------------------------------
  29.  
  30. void CString::InsertHelper(const CString& insStr,
  31.                           short pos,
  32.                           short maxLength)
  33. {
  34.     if (pos > Length() + 1)
  35.     {
  36. #if qDebugMsg
  37.         fprintf(stderr, "###CString::InsertHelper: Insert position greater than length of CString.\n");
  38. #endif
  39.         if (Length() < maxLength)    
  40.             pos = Length() + 1;
  41.     }
  42.     
  43. #if qDebugMsg
  44.     if (Length() + insStr.Length() > maxLength)
  45.         fprintf(stderr, "### CString::InsertHelper: CString truncated during insert call.\n");    
  46. #endif
  47.  
  48.     short usableLengthOfInsertString;
  49.     short endPosOfInsertString;
  50.     short usableLengthOfShiftedString;
  51.     
  52.     if (pos + insStr.Length() > maxLength)
  53.         usableLengthOfInsertString = maxLength - pos + 1;
  54.     else
  55.         usableLengthOfInsertString = insStr.Length();
  56.     endPosOfInsertString = pos + usableLengthOfInsertString - 1;
  57.     
  58.     if ((endPosOfInsertString + 1) + (Length() - pos + 1) > maxLength)
  59.         usableLengthOfShiftedString = maxLength - endPosOfInsertString;
  60.     else
  61.         usableLengthOfShiftedString = Length() - pos + 1;
  62.         
  63.     memmove(&fStr[endPosOfInsertString + 1], &fStr[pos], usableLengthOfShiftedString);
  64.     memmove(&fStr[pos], &insStr.fStr[1], usableLengthOfInsertString);
  65.     Length() = usableLengthOfShiftedString + endPosOfInsertString;
  66. } // CString::InsertHelper(CString)
  67.  
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // CString::InsertHelper(char*):
  71. //----------------------------------------------------------------------------------------
  72.  
  73. void CString::InsertHelper(const char* insStr,
  74.                           short pos,
  75.                           short maxLength)
  76. {
  77.     this->InsertHelper(CStr255(insStr), pos, maxLength);
  78. } // CString::InsertHelper(char*)
  79.  
  80.  
  81. //----------------------------------------------------------------------------------------
  82. // CString::operator[]: !!! we'd ideally like this method to be inlined but CFront doesn't
  83. // seem to want to inline it for us !!!
  84. //----------------------------------------------------------------------------------------
  85.  
  86. unsigned char& CString::operator[](short pos)
  87. {
  88.     return fStr[pos];
  89. } // CString::operator[] for non-const CString
  90.  
  91.  
  92. //----------------------------------------------------------------------------------------
  93. // CString::operator char*:
  94. //----------------------------------------------------------------------------------------
  95. CString::operator char*() const
  96. {
  97.     const short kTempCStrings = 4;
  98.     static short currentCString = 0;
  99.     static char cStrings[kTempCStrings][kStr255Len+1];
  100.     
  101.     currentCString = (currentCString + 1) % kTempCStrings;
  102.     
  103.     strncpy(cStrings[currentCString], (char *) &fStr[1], Length());
  104.     cStrings[currentCString][Length()] = '\0';
  105.     
  106.     return cStrings[currentCString];
  107. } // CString::operator char*
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // CString::operator long:
  111. //----------------------------------------------------------------------------------------
  112.  
  113. CString::operator long() const
  114. {
  115.     // The following statement looks like it should work. Right?
  116.     //
  117.     //    return *((long *) &fStr[1]);
  118.     //
  119.     // Wrong, the C compiler generates a MOVE.L starting on a odd byte boundary for the
  120.     // preceding statement. This is illegal on the 68000. But its _NOT_ a bug, because
  121.     // according to the ANSI C reference manual, "A pointer to one type may be converted
  122.     // to a pointer to another type. The resulting pointer may cause an addressing
  123.     // exception if the subject pointer does not refer to an object suitably aligned in
  124.     // storage".
  125.     
  126.     long returnLong;
  127.     
  128.     memcpy(&returnLong, &fStr[1], sizeof(long));
  129.     return returnLong;
  130. } // CString::operator long
  131.  
  132.  
  133. //----------------------------------------------------------------------------------------
  134. // CString::Pos(char*):
  135. //----------------------------------------------------------------------------------------
  136.  
  137. unsigned char CString::Pos(const char* subStr, unsigned char startPos)
  138. {
  139.     char cStr[kStr255Len + 1];
  140.     char* ptr;
  141.     
  142.     memcpy(cStr, &fStr[1], Length());
  143.     cStr[Length()] = 0;
  144.     ptr = strstr(&cStr[startPos - 1], subStr);
  145.     return ptr != NULL ? (ptr - cStr) + 1 : 0;
  146. } // CString::Pos(char*)
  147.  
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // CString::Pos(CString):
  151. //----------------------------------------------------------------------------------------
  152.  
  153. unsigned char CString::Pos(const CString& subStr, unsigned char startPos)
  154. {
  155.     char cStr[kStr255Len + 1];
  156.  
  157.     memcpy(cStr, &subStr.fStr[1], subStr.Length());
  158.     cStr[subStr.Length()] = 0;
  159.     return this->Pos(cStr, startPos);
  160. } // CString::Pos(CString)
  161.  
  162.  
  163.  
  164. //========================================================================================
  165. // CLASS CStr255
  166. //========================================================================================
  167.  
  168.  
  169. //----------------------------------------------------------------------------------------
  170. // CStr255::CStr255(char*):
  171. //----------------------------------------------------------------------------------------
  172.  
  173. CStr255::CStr255(const char* str)
  174. {
  175.     // Truncate the C CString to 255 bytes if necessary.
  176.  
  177.     Length() = str == NULL ? 0 : strlen(str);
  178.     
  179.     if (Length() > kStr255Len)
  180.         Length() = kStr255Len;
  181.     memcpy(&fStr[1], str, Length());
  182. } // CStr255::CStr255(char*)
  183.  
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // CStr255::CStr255(long): Useful for converting OSType's into CStr255's.
  187. //----------------------------------------------------------------------------------------
  188.  
  189. CStr255::CStr255(const long id)
  190. {
  191.     Length() = 4;
  192.     memcpy(&fStr[1], &id, Length());
  193. } // CStr255::CStr255(long)
  194.  
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // CStr255::Copy:
  198. //----------------------------------------------------------------------------------------
  199.  
  200. CStr255 CStr255::Copy(short pos, short length)
  201. {
  202.     CStr255 newString;
  203.     
  204.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  205.     
  206.     if (length > 0)
  207.     {
  208.         memcpy(&newString.fStr[1], &fStr[pos], length);
  209.         newString.Length() = length;
  210.     }
  211.     else
  212.         newString = "";
  213.         
  214.     return newString;
  215.     
  216. } // CStr255::Copy
  217.  
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // CStr255::operator+:
  221. //----------------------------------------------------------------------------------------
  222.  
  223. CStr255 operator+(const CString& s1,
  224.                   const char* s2)
  225. {
  226.     CStr255 newStr;
  227.     short s2Len = s2 == NULL ? 0 : strlen((const char *) s2);
  228.  
  229.     if (s1.Length() + s2Len > kStr255Len)
  230.         newStr.Length() = kStr255Len;
  231.     else
  232.         newStr.Length() = s1.Length() + s2Len;
  233.         
  234.     memcpy(&newStr.fStr[1], &s1.fStr[1], s1.Length());
  235.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2, newStr.Length() - s1.Length());
  236.  
  237.     return newStr;
  238. } // CStr255::operator+
  239.  
  240.  
  241. //----------------------------------------------------------------------------------------
  242. // CStr255::operator+(char*,CString):
  243. //----------------------------------------------------------------------------------------
  244.  
  245. CStr255 operator+(const char* s1,
  246.                   const CString& s2)
  247. {
  248.     CStr255 newStr;
  249.     short s1Len = s1 == NULL ? 0 : strlen((const char *) s1);
  250.  
  251.     if (s1Len + s2.Length() > kStr255Len)
  252.         newStr.Length() = kStr255Len;
  253.     else
  254.         newStr.Length() = s1Len + s2.Length();
  255.         
  256.     memcpy(&newStr.fStr[1], s1, s1Len);
  257.     memcpy(&newStr.fStr[s1Len + kLengthByte], s2.fStr + 1, newStr.Length() - s1Len);
  258.  
  259.     return newStr;
  260. } // CStr255::operator+(char*,CString)
  261.  
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // CStr255::operator+(CString,CString):
  265. //----------------------------------------------------------------------------------------
  266.  
  267. CStr255 operator+(const CString& s1,
  268.                   const CString& s2)
  269. {
  270.     CStr255 newStr;
  271.  
  272.     if (s1.Length() + s2.Length() > kStr255Len)
  273.         newStr.Length() = kStr255Len;
  274.     else
  275.         newStr.Length() = s1.Length() + s2.Length();
  276.         
  277.     memcpy(&newStr.fStr[1], &s1.fStr[1], s1.Length());
  278.     memcpy(&newStr.fStr[s1.Length() + kLengthByte], s2.fStr + 1, newStr.Length() - s1.Length());
  279.  
  280.     return newStr;
  281. } // CStr255::operator+(CString,CString)
  282.  
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // CStr255::operator +=(CString):  Concatinate a string
  286. //----------------------------------------------------------------------------------------
  287.  
  288. CStr255& CStr255::operator += (const CString& str)
  289. {
  290.     InsertHelper (str, Length() + 1, kStr255Len);
  291.     return *this;
  292. } // CStr255::operator +=(CString)
  293.  
  294.  
  295. //----------------------------------------------------------------------------------------
  296. // CStr255::operator +=(char*):  Concatinate a string
  297. //----------------------------------------------------------------------------------------
  298.  
  299. CStr255& CStr255::operator += (const char* str)
  300. {
  301.     InsertHelper (str, Length() + 1, kStr255Len);
  302.     return *this;
  303. } // CStr255::operator +=(char*)
  304.  
  305.  
  306. //----------------------------------------------------------------------------------------
  307. // CStr255::operator +=(char):  Concatinate a single character
  308. //----------------------------------------------------------------------------------------
  309.  
  310. CStr255& CStr255::operator += (const char ch)
  311. {
  312.     if (++Length() <= kStr255Len)
  313.         fStr[Length()] = ch;
  314.     else
  315.     {
  316.         --Length();
  317. #if qDebugMsg
  318.         fprintf(stderr, "###CStr255::operator+=: Concatenation produces CStr255 overflow.\n");
  319. #endif
  320.     }
  321.     
  322.     return *this;
  323. } // CStr255::operator +=(char)
  324.  
  325.  
  326. //----------------------------------------------------------------------------------------
  327. // CStr255::operator =:
  328. //----------------------------------------------------------------------------------------
  329.  
  330. CStr255& CStr255::operator = (const char* str)
  331. {
  332.     if (str)
  333.     {
  334.         // Truncate the C CString to 255 bytes if necessary.
  335.         register size_t itsSize = strlen(str);
  336.         if (itsSize > kStr255Len)
  337.             Length() = kStr255Len;
  338.         else
  339.             Length() = itsSize;
  340.  
  341.         memcpy(&fStr[1], str, Length());
  342.     }
  343.     else
  344.         Length() = 0;
  345.     
  346.     return *this;
  347. } // CStr255::operator =
  348.  
  349.  
  350.  
  351. //========================================================================================
  352. // CLASS CStr63
  353. //========================================================================================
  354.  
  355.  
  356. //----------------------------------------------------------------------------------------
  357. // CStr63::CStr63(char*):
  358. //----------------------------------------------------------------------------------------
  359.  
  360. CStr63::CStr63(const char* str)
  361. {
  362.     // Truncate the C CString to 63 bytes if necessary.
  363.  
  364.     Length() = str == NULL ? 0 : strlen((const char*)str);
  365.     if (Length() > kStr63Len)
  366.         Length() = kStr63Len;
  367.     memcpy(&fStr[1], str, Length());
  368. } // CStr63::CStr63(char*)
  369.  
  370.  
  371. //----------------------------------------------------------------------------------------
  372. // CStr63::CStr63(long):
  373. //----------------------------------------------------------------------------------------
  374.  
  375. CStr63::CStr63(const long id)
  376. {
  377.     Length() = 4;
  378.     memcpy(&fStr[1], &id, Length());
  379. } // CStr63::CStr63(long)
  380.  
  381.  
  382. //----------------------------------------------------------------------------------------
  383. // CStr63::Copy:
  384. //----------------------------------------------------------------------------------------
  385.  
  386. CStr63 CStr63::Copy(short pos, short length)
  387. {
  388.     CStr63 newString;
  389.     
  390.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  391.     
  392.     if (length > 0)
  393.     {
  394.         memcpy(&newString.fStr[1], &fStr[pos], length);
  395.         newString.Length() = length;
  396.     }
  397.     else
  398.         newString = "";
  399.         
  400.     return newString;
  401.     
  402. } // CStr63::Copy
  403.  
  404.  
  405. //----------------------------------------------------------------------------------------
  406. // CStr63::operator +=(CString):  Concatinate a string
  407. //----------------------------------------------------------------------------------------
  408.  
  409. CStr63& CStr63::operator += (const CString& str)
  410. {
  411.     InsertHelper (str, Length() + 1, kStr63Len);
  412.     return *this;
  413. } // CStr63::operator +=(CString)
  414.  
  415.  
  416. //----------------------------------------------------------------------------------------
  417. // CStr63::operator +=(char*):  Concatinate a string
  418. //----------------------------------------------------------------------------------------
  419.  
  420. CStr63& CStr63::operator += (const char* str)
  421. {
  422.     InsertHelper (str, Length() + 1, kStr63Len);
  423.     return *this;
  424. } // CStr63::operator +=(char*)
  425.  
  426.  
  427. //----------------------------------------------------------------------------------------
  428. // CStr63::operator +=(char):  Concatinate a single character
  429. //----------------------------------------------------------------------------------------
  430.  
  431. CStr63& CStr63::operator += (const char ch)
  432. {
  433.     if (++Length() <= kStr63Len)
  434.         fStr[Length()] = ch;
  435.     else
  436.     {
  437.         --Length();
  438. #if qDebugMsg
  439.         fprintf(stderr, "###CStr63::operator+=: Concatenation produces CStr63 overflow.\n");
  440. #endif
  441.     }
  442.     
  443.     return *this;
  444. } // CStr63::operator +=(char)
  445.  
  446.  
  447.  
  448. //========================================================================================
  449. // CLASS CStr32
  450. //========================================================================================
  451.  
  452.  
  453. //----------------------------------------------------------------------------------------
  454. // CStr32::CStr32(char*):
  455. //----------------------------------------------------------------------------------------
  456.  
  457. CStr32::CStr32(const char* str)
  458. {
  459.     // Truncate the C CString to 32 bytes if necessary.
  460.  
  461.     Length() = str == NULL ? 0 : strlen((const char*)str);
  462.     if (Length() > kStr32Len)
  463.         Length() = kStr32Len;
  464.     memcpy(&fStr[1], str, Length());
  465. } // CStr32::CStr32(char*)
  466.  
  467.  
  468. //----------------------------------------------------------------------------------------
  469. // CStr32::CStr32(long):
  470. //----------------------------------------------------------------------------------------
  471.  
  472. CStr32::CStr32(const long id)
  473. {
  474.     Length() = 4;
  475.     memcpy(&fStr[1], &id, Length());
  476. } // CStr32::CStr32(long)
  477.  
  478.  
  479. //----------------------------------------------------------------------------------------
  480. // CStr32::Copy:
  481. //----------------------------------------------------------------------------------------
  482.  
  483. CStr32 CStr32::Copy(short pos, short length)
  484. {
  485.     CStr32 newString;
  486.     
  487.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  488.     
  489.     if (length > 0)
  490.     {
  491.         memcpy(&newString.fStr[1], &fStr[pos], length);
  492.         newString.Length() = length;
  493.     }
  494.     else
  495.         newString = "";
  496.         
  497.     return newString;
  498.  
  499. } // CStr32::Copy
  500.  
  501.  
  502.  
  503. //========================================================================================
  504. // CLASS CStr31
  505. //========================================================================================
  506.  
  507.  
  508. //----------------------------------------------------------------------------------------
  509. // CStr31::CStr31(char*):
  510. //----------------------------------------------------------------------------------------
  511.  
  512. CStr31::CStr31(const char* str)
  513. {
  514.     // Truncate the C CString to 31 bytes if necessary.
  515.  
  516.     Length() = str == NULL ? 0 : strlen((const char*)str);
  517.     if (Length() > kStr31Len)
  518.         Length() = kStr31Len;
  519.     memcpy(&fStr[1], str, Length());
  520. } // CStr31::CStr31(char*)
  521.  
  522.  
  523. //----------------------------------------------------------------------------------------
  524. // CStr31::CStr31(long):
  525. //----------------------------------------------------------------------------------------
  526.  
  527. CStr31::CStr31(const long id)
  528. {
  529.     Length() = 4;
  530.     memcpy(&fStr[1], &id, Length());
  531. } // CStr31::CStr31(long)
  532.  
  533.  
  534. //----------------------------------------------------------------------------------------
  535. // CStr31::Copy:
  536. //----------------------------------------------------------------------------------------
  537.  
  538. CStr31 CStr31::Copy(short pos, short length)
  539. {
  540.     CStr31 newString;
  541.     
  542.     length = length > Length() - pos + kLengthByte ? Length() - pos + kLengthByte : length;
  543.     
  544.     if (length > 0)
  545.     {
  546.         memcpy(&newString.fStr[1], &fStr[pos], length);
  547.         newString.Length() = length;
  548.     }
  549.     else
  550.         newString = "";
  551.         
  552.     return newString;
  553.  
  554. } // CStr31::Copy
  555.  
  556.  
  557. //----------------------------------------------------------------------------------------
  558. // CStr31::operator +=(CString):  Concatinate a string
  559. //----------------------------------------------------------------------------------------
  560.  
  561. CStr31& CStr31::operator += (const CString& str)
  562. {
  563.     InsertHelper (str, Length() + 1, kStr31Len);
  564.     return *this;
  565. } // CStr31::operator +=(CString)
  566.  
  567.  
  568. //----------------------------------------------------------------------------------------
  569. // CStr31::operator +=(char*):  Concatinate a string
  570. //----------------------------------------------------------------------------------------
  571.  
  572. CStr31& CStr31::operator += (const char* str)
  573. {
  574.     InsertHelper (str, Length() + 1, kStr31Len);
  575.     return *this;
  576. } // CStr31::operator +=(char*)
  577.  
  578.  
  579. //----------------------------------------------------------------------------------------
  580. // CStr31::operator +=(char):  Concatinate a single character
  581. //----------------------------------------------------------------------------------------
  582.  
  583. CStr31& CStr31::operator += (const char ch)
  584. {
  585.     if (++Length() <= kStr31Len)
  586.         fStr[Length()] = ch;
  587.     else
  588.     {
  589.         --Length();
  590. #if qDebugMsg
  591.         fprintf(stderr,"###CStr31::operator+=: Concatenation produces CStr31 overflow.\n");
  592. #endif
  593.     }
  594.     
  595.     return *this;
  596. } // CStr31::operator +=(char)
  597.